<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/7
 * Time: 16:20
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;

class Template extends AdminBase
{
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            $list = Db::name('url_rule')->select();
            return view('index', [
                'list'=>$list
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }

    }

    public function add()
    {
        $chk = check_juri(request()->controller(), 'add', $this->_juri);
        if ($chk['status'])
        {
            return view('add');
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }

    }

    public function doAdd()
    {
        if (request()->isPost())
        {
            $data                       = [];
            $data['moudle']             = input('param.moudle');
            $data['controller']         = input('param.controller');
            $data['action']             = input('param.action');
            $data['tpl']                = json_encode(input('param.tpl/a'));
            $data['remark']             = input('param.remark');
            $data['status']             = intval(input('param.status'));
            $data['create_time']        = time();
            $data['update_time']        = $data['create_time'];
            $res = Db::name('url_rule')->insertGetId($data);
            if ($res)
            {
                return ['status'=>true, 'msg'=>'添加成功'];
            } else {
                return ['status'=>false, 'msg'=>'添加失败'];
            }
        }
    }

    public function edit()
    {
        $chk = check_juri(request()->controller(), 'edit', $this->_juri);
        if ($chk['status'])
        {
            $id   = intval(input('param.id'));
            $info = Db::name('url_rule')
                ->field('id, moudle, controller, action, tpl, remark, status')
                ->where('id', 'eq', $id)
                ->find();
            $info['tpl'] = json_decode($info['tpl'], true);
            return view('edit', [
                'info'=>$info
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }

    }

    public function doEdit()
    {
        if (request()->isPost())
        {
            $id                         = intval(input('param.id'));
            $data                       = [];
            $data['moudle']             = input('param.moudle');
            $data['controller']         = input('param.controller');
            $data['action']             = input('param.action');
            $data['tpl']                = json_encode(input('param.tpl/a'));
            $data['remark']             = input('param.remark');
            $data['status']             = intval(input('param.status'));
            $data['update_time']        = time();
            $res = Db::name('url_rule')->where('id', 'eq', $id)->update($data);
            if ($res)
            {
                return true;
            } else {
                return false;
            }
        }
    }

    public function del()
    {
        if (request()->isPost())
        {
            $chk = check_juri(request()->controller(), 'del', $this->_juri);
            if ($chk['status'])
            {
                $id   = intval(input('param.id'));
                $res = Db::name('url_rule')->where('id', 'eq', $id)->delete();
                if ($res)
                {
                    return ['status'=>true, 'msg'=>'删除成功'];
                } else {
                    return ['status'=>false, 'msg'=>'删除失败'];
                }
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }

    public function updateRule()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'updateRule', $this->_juri);
            if ($chk['status'])
            {
                $str     = "<?php\n";
                $str    .= "return [\n";
                $rules   = Db::name('url_rule')
                    ->field('id, moudle, controller, action, tpl')
                    ->where('status', 1)
                    ->select();
                foreach ($rules as $key => $value) {
                    $value['tpl'] = json_decode($value['tpl'], true);
                    foreach ($value['tpl'] as $val) {
                        $str  .= "'runtime/{$value['moudle']}/{$val}_<id?>'       => ['{$value['moudle']}/{$value['controller']}/{$value['action']}', ['method'=>'get'], ['id'=>'\d+']],\n";
                    }
                }
                $str   .= "];";
                $url1   = CMS_ROOT.'config/route.php';
                file_put_contents($url1, $str);
                return ['status'=>true, 'msg'=>''];
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }
        }
    }
}